// packageA/pages/attendancePersonDay/attendancePersonDay.js
import CommonService from '../../../utils/service/commonService.js';
const util = require('../../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //传参
    paramObj:null,
    //当前月份
    currentMonth: new Date().getMonth() + 1,
    //查询的日期
    currentDate:util.timeFormatDay(new Date()),
    //明天
    tomorrow:null,
    //打卡记录
    clockRecord:[],
    //月统计
    lateNum:0,
    leaveEarlyNum:0,
    missingNum:0,
    //考勤组和排班信息
    attendanceInfo: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.paramObj){
      let paramObj=JSON.parse(options.paramObj);
      console.log('人员日统计传参');
      console.log(paramObj);

      this.setData({
        paramObj: paramObj
      })  

      //获取个人月统计数据
      this.getMonth();
    }
  },

  //获取选择日期
  bindgetdate(e) {
    let dd = e.detail;

    //计算明天
    let m=parseInt(dd.month)-1;
    let d=parseInt(dd.date)+1;
    let tomorrow=util.timeFormatDay(new Date(dd.year, m, d));

    this.setData({
      currentDate: dd.year + '-' + dd.month + '-' + dd.date,
      tomorrow: tomorrow
    })

    //获取考勤组和排班信息
    this.getAttendanceInfo();

    //获取个人日统计数据
    this.getDay();
  },

  //获取个人日统计数据
  getDay(){
    this.setData({
      clockRecord: []
    })

    let param={
      imei: this.data.paramObj.imei,
      startTime: this.data.currentDate,
      endTime: this.data.tomorrow
    }

    CommonService.findOneClockIn(param).then(res=>{
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let result=res.data.data.result[0];

        console.log('日统计');
        console.log(result);  

        if(result.clockRecord.length){
          let clockRecord=result.clockRecord;
          clockRecord.forEach(item => {
            if(item.punchInTime){
              //打卡存的是本地时间，但搞成了UTC格式，所以无需工具转化，直接截取即可
              item.punchInTime=item.punchInTime.substring(11,19);
            }
          });

          this.setData({
            clockRecord: clockRecord
          })
        }
      }
    }).catch((error) => {
    })
  },

  //获取个人月统计数据
  getMonth(){
    let start=util.timeFormatMonth2(this.data.currentDate)+'-01';

    //计算下个月
    let time = new Date(this.data.currentDate);
    let year = time.getFullYear();
    let month = time.getMonth() + 2;
    if(month > 12) {
      month = month - 12;
      year = year + 1;
    }
    if(month < 10) {
      month = '0' + month;
    }
    let end = year + '-' + month + '-01';

    let param={
      imei: this.data.paramObj.imei,
      startTime: start,
      endTime: end
    }

    CommonService.findAbnormalClockIn(param).then(res=>{
      if(res.data.data && res.data.data.result){
        let result=res.data.data.result;

        console.log('月统计');
        console.log(result);

        if(result.late && result.late.count){
          this.setData({
            lateNum:result.late.count
          })
        }

        if(result.leaveEarly && result.leaveEarly.count){
          this.setData({
            leaveEarlyNum:result.leaveEarly.count
          })
        }

        if(result.missing && result.missing.count){
          this.setData({
            missingNum:result.missing.count
          })
        }
      }
    }).catch((error) => {
    })
  },

  //跳转个人月统计
  goToMonth(){
    let obj={
      imei: this.data.paramObj.imei,
      nickName: this.data.paramObj.nickName
    }
    wx.navigateTo({
      url: "/packageA/pages/attendancePersonMonth/attendancePersonMonth?paramObj="+JSON.stringify(obj)
    })
  },

  //获取人员信息
  getInfo(){
    let param = {
      imei: this.data.paramObj.imei 
    }

    CommonService.findPersonnel(param).then(res => {
      if(res.data.data && res.data.data.result && res.data.data.result) {
        let result=res.data.data.result;
        console.log('人员信息');
        console.log(result); 
        
        this.setData({
          userInfo: result
        })
      }
    }).catch(() => {
    })
  },

  //获取考勤组和排班信息
  getAttendanceInfo(){
    this.setData({
      attendanceInfo: null
    })

    let param={
      imei: this.data.paramObj.imei,
      beginDate: this.data.currentDate,
      endDate: this.data.currentDate
    }

    CommonService.attendanceStatisticsByDay(param).then(res => {
      if(res.data.data && res.data.data.result && res.data.data.result) {
        let result=res.data.data.result[0];
        console.log('考勤组和排班信息');
        console.log(result);

        //处理排班的时间
        if(result.workOverRecord && result.workOverRecord.length){
          result.workOverRecord.forEach(item => {
            item.self_start=item.startTime.substring(11,16);
            item.self_end=item.endTime.substring(11,16);
          });
        }

        this.setData({
          attendanceInfo: result
        }) 
      }
    }).catch(() => {
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
